/**
 * 
 */
package com.maywide.jdp.system.aspect;

import java.util.List;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.jboss.cache.Cache;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.maywide.jdp.system.bean.OrganizationBean;
import com.maywide.jdp.system.bean.ParameterBean;
import com.maywide.jdp.system.entities.Organization;
import com.maywide.jdp.system.entities.Parameter;

/**
 * @author luoyang
 */
@Aspect
@Component("parameterAspect")
public class ParameterAspect {
	private Cache<String, Object> cache = null;
	
	private ParameterBean parameterBean = null;

	private OrganizationBean organizationBean = null;
	
	@Resource(name = "cache")
	public void setCache(Cache<String, Object> cache) {
		this.cache = cache;
	}

	@Resource(name = "parameterBean")
	public void setParameterBean(ParameterBean parameterBean) {
		this.parameterBean = parameterBean;
	}

	@Resource(name = "organizationBean")
	public void setOrganizationBean(OrganizationBean organizationBean) {
		this.organizationBean = organizationBean;
	}

	@SuppressWarnings("unused")
	@Order(100)
	@AfterReturning(pointcut="execution(public * com.maywide.jdp.system.service.ParameterService.insert(..)) and args(parameter,..)",returning="retVal",argNames="retVal,parameter")
	private void doInsert(Object retVal,Parameter parameter) throws Exception {
		String entityID = parameter.getEntity().getId();
		Organization entity = (Organization) organizationBean.load(entityID);
		String path = entity.getCodePath();
		String key = parameter.getCode();
		String value = parameter.getData();
		cache.put(path, key, value);
	}

	@SuppressWarnings("unused")
	@Order(100)
	@AfterReturning(pointcut="execution(public * com.maywide.jdp.system.service.ParameterService.update(..)) and args(parameterVO,..)",returning="retVal",argNames="retVal,parameterVO")
	private void doUpdate(Object retVal,Parameter parameterVO) throws Exception {
		Parameter ret = (Parameter) retVal;
		String path = ret.getEntity().getCodePath();
		String key = parameterVO.getCode();
		String value = parameterVO.getData();
		cache.put(path, key, value);
	}

	@SuppressWarnings("unused")
	@Order(100)
	@AfterReturning(pointcut="execution(public * com.maywide.jdp.system.service.ParameterService.remove(..)) and args(id,..)",argNames="id")
	private void doRemove(JoinPoint jp, String id) throws Exception {
		Parameter parameter = (Parameter) parameterBean.load(id);
		String path = new StringBuffer(Organization.ROOT_CODE_TEXT).append(parameter.getEntity().getCodePath()).toString();
		String key = parameter.getCode();
		cache.remove(path, key);
	}

	@SuppressWarnings("unused")
	@Order(100)
	@AfterReturning(pointcut="execution(public * com.maywide.jdp.system.service.ParameterService.move(..)) and args(ids,oldOrgID,newOrgID,..)",argNames="ids,oldOrgID,newOrgID")
	private void doMove(JoinPoint jp, List<String> ids, String oldOrgID, String newOrgID) throws Exception {
		Organization newOrg = (Organization) organizationBean.load(newOrgID);
		Organization oldOrg = (Organization) organizationBean.load(oldOrgID);
		String oldPath = new StringBuffer(Organization.ROOT_CODE_TEXT).append(oldOrg.getCodePath()).toString();
		String newPath = new StringBuffer(Organization.ROOT_CODE_TEXT).append(newOrg.getCodePath()).toString();
		for(String id : ids) {
			Parameter parameter = (Parameter) parameterBean.load(id);
			String key = parameter.getCode();
			String data = parameter.getData();
			cache.remove(oldPath, key);
			cache.put(oldPath, key, data);
		}
	}
}
